package net.bplaced.streetfighter.fragment;

import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.model.AppVersion;
import net.bplaced.streetfighter.lib.utils.RuntimeData;
import net.bplaced.streetfighter.lib.utils.RuntimeDataKey;
import net.bplaced.streetfighter.update.UpdateInfoAdapter;
import android.app.DownloadManager;
import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class UpdatesFragment
        extends Fragment {

    private RelativeLayout view = null;

    private UpdateInfoAdapter adapter;

    private int iconDrawableId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        this.view = (RelativeLayout) inflater.inflate(R.layout.view_updates, container, false);

        this.prepareView();

        return this.view;
    }

    private void prepareView() {

        this.getActivity().setTitle("App Update");
        this.iconDrawableId = R.drawable.ic_launcher;

        AppVersion[] versions = null;
        if (RuntimeData.getInstance().get(RuntimeDataKey.UPDATES) != null) {

            versions = (AppVersion[]) RuntimeData.getInstance().get(RuntimeDataKey.UPDATES).getData();
        }
        this.adapter = new UpdateInfoAdapter(this.getActivity(), versions);
        final ListView listView = (ListView) this.view.findViewById(android.R.id.list);
        listView.setDivider(null);
        listView.setAdapter(this.adapter);
        final ImageButton updateButton = (ImageButton) this.view.findViewById(R.id.update_button);
        updateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                UpdatesFragment.this.startDownloadTask();
            }
        });
        this.configureView();
    }

    private void configureView() {

        if (this.iconDrawableId != -1) {
            final ImageView iconView = (ImageView) this.view.findViewById(R.id.icon_view);
            iconView.setImageDrawable(this.getResources().getDrawable(this.iconDrawableId));
        }

        final TextView versionLabel = (TextView) this.view.findViewById(R.id.version_label);
        versionLabel.setText("Version " + this.adapter.getLatestVersion().versionName);
    }

    private void startDownloadTask() {

        final DownloadManager.Request request =
                new DownloadManager.Request(Uri.parse(this.adapter.getLatestVersion().downloadLink));
        request.setTitle("Streetfighter.apk");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Streetfighter.apk");
        final DownloadManager manager = (DownloadManager) this.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }
}